﻿#region License, Terms and Conditions
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
#endregion
using System;
using System.Text.RegularExpressions;

namespace raya.Library.Helpers
{
    public static class PHPExt
    {
        private static readonly Regex _isNumber = new Regex(@"^\d+_", RegexOptions.Compiled);

        public static bool is_numeric(string anumber)
        {
            //Match m = _isNumber.Match(anumber);
            //return m.Success;
            int val;
            return int.TryParse(anumber, out val);
        }

        public static bool empty(string value)
        {
            return String.IsNullOrEmpty(value);
        }
        
        public static bool isset(object value)
        {
            return value != null;
        }

        /// <summary>
        /// make first character upper case
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ucwords(string str)
        {
            return Regex.Replace(str, @"\b[a-z]\w+", new MatchEvaluator(ucwordsMatcher));

        }

        static string ucwordsMatcher(Match m)
        {
            string v = m.ToString();
            return char.ToUpper(v[0]) + v.Substring(1);
        }

        static public string base64_encode(string str)
        {
            byte[] encbuff = System.Text.Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(encbuff);
        }
        static public string base64_decode(string str)
        {
            byte[] decbuff = Convert.FromBase64String(str);
            return System.Text.Encoding.UTF8.GetString(decbuff);
        }

        static public DateTime ToDateTime(double timestamp)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return origin.AddSeconds(timestamp);
        }

        static public double ToDouble(DateTime date)
        {
            DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            TimeSpan diff = date - origin;
            return Math.Floor(diff.TotalSeconds);
        }

        static public string strip_tags(string html)
        {
            return Regex.Replace(html, @"<(.|\n)*?>", string.Empty);
        }
    }
}
